package huizhi.exercise;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        //绝对路径
        fileTreeView("C:/Users/Administrator/Desktop/后端");
    }

    private static void fileTreeView(String url) {
        File file = new File(url);
        fileTreeView2(file.getAbsolutePath(), 0);
    }

    private static void fileTreeView2(String url, int indentation) {
        File file = new File(url);
        File[] files = file.listFiles();

        if (files != null) {
            List<String> directories = new ArrayList<>();
            List<String> regularFiles = new ArrayList<>();

            for (File file1 : files) {
                if (file1.isDirectory()) {
                    directories.add(file1.getName());
                } else if (file1.isFile()) {
                    regularFiles.add(file1.getName());
                }
            }

            // Print directories
            for (String directory : directories) {
                printIndented(indentation, "^" + directory);
                fileTreeView2(file.getAbsolutePath() + File.separator + directory, indentation + 2);
            }

            // Print files
            for (String regularFile : regularFiles) {
                printIndented(indentation, regularFile);
            }
        }
    }

    private static void printIndented(int indentation, String content) {
        for (int i = 0; i < indentation; i++) {
            System.out.print(" ");
        }
        System.out.println(content);
    }
}
